package fr.labri.macchiato.feature.localRepositoryJson;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;

import fr.labri.macchiato.core.technical.Encoder;


/**
 * Encodes and decode a {@link List}< String > in JSON
 * 
 * @author Matthieu FOUCAULT
 * 
 */
public class StringListEncoder implements Encoder<List<String>> {

	public String encode(List<String> object) {
		JSONArray jsArray = new JSONArray();
		int i = 0;
		for (String item : object) {
			jsArray.set(i++, new JSONString(item));

		}

		return jsArray.toString();
	}

	public List<String> decode(String jsonString) {
		JSONArray jsArray = JSONParser.parseStrict(jsonString).isArray();
		List<String> result = new ArrayList<String>(jsArray.size());
		for (int i = 0; i < jsArray.size(); i++)
			result.add(jsArray.get(i).isString().stringValue());

		return result;
	}

	public List<List<String>> decodeSparqlResult(String jsonString) {
		// TODO : decode SparqlResult
		return null;
	}
}
